<?php 

/**
 * User: wu
 * Date: 2019/12/12
 * time: 17:00
 * 
 */
namespace app\common;

/**
 * 公用sql
 */
class TableTool
{    

    
    /** 
     * 获取单条数据
     *
     * @param table string  表名
     * @param where array 条件
     * @param field string 返回字段 
     *
     */
    public function find($table,$where = [],$field = '*')
    {    
        global $db;
        try {
            return $db->select($field)->from($table)->where($where)->row();
        } catch (Exception $e) {
            return false;
        }
    }
 


    /**
     * 查询多条数据
     *
     * @param table string  表名
     * @param where array 条件
     * @param field string 返回字段 
     * @param page  int  当前页
     * @param count int 每页条数
     * @param order array 带字段过来则倒序
     *
     */
    public function select($table,$where = [],$field = '*',$page = 1,$count = 10,$order = [])
    {   
        global $db;
        try {
            if(empty($where)){
                return $db->select($field)->from($table)->page($page,$count)
                   ->orderByDESC($order)->query();
            }else{
                return $db->select($field)->from($table)
                   ->where($where)->page($page,$count)
                   ->orderByDESC($order)->query();
            }
            
        } catch (Exception $e) {
            return false;
        }
    }



    /**
     * 插入
     *
     * @param table string  表名
     * @param data  array 插入的数据
     *
     */
    public function insert($table,$data = [])
    {
        global $db;
        try {
            return $db->insert($table)->cols($data)->query();
        } catch (Exception $e) {
            return false;
        }
    }



    /**
     * 更新
     *
     * @param table string  表名
     * @param where array 条件
     * @param data  array 要更新的数据
     *
     */
    public function update($table,$where = [],$data = [])
    {
        global $db;
        try {
            $db->update($table)->cols($data)->where($where)->query();
            return true;
        } catch (Exception $e) {
            return false;
        }
        
    }



    /**
     * 删除
     *
     * @param table string  表名
     * @param where array 条件
     *
     */
    public function delete($table,$where = [])
    {
        global $db;
        try {
            return $db->delete($table)->where($where)->query();
        } catch (Exception $e) {
            return false;
        }
    }



    /**
     * 获取总数量
     *
     * @param table string  表名
     * @param where array 条件
     *
     */
    public function getCount($table,$where = []){
        global $db;
        try {
            return $db->select('count(*) as count')->from($table)->where($where)->query();
        } catch (Exception $e) {
            return false;
        }
    }



    /**
     * 获取地区范围id
     *
     */
    public function getRegionIds($id = ''){
        global $db;
        if(empty($id)){
            return false;
        }
        $region = explode(',',$id);
        if(count($region) == 1){
            $region_info = $this->find("region",['id'=>$id],"id,region_type");
            switch ($region_info['region_type']) {
                case 0:
                    $field = "*";
                    break;
                case 1:
                    $field = "province_id";
                    break;
                case 2:
                    $field = "city_id";
                    break;
                case 3:
                    $field = "district_id";
                    break;
            }
            if($field == "*"){
                $region_ids = "all";
            }else{
                $region_ids = $this->select("region",[$field => $id],"id",1,10000);
                $region_ids = implode(',',array_column($region_ids,"id"));
            }
            return $region_ids;
        }
        foreach ($region as $k => $v) {
            if($v == 100000){
                return 'all';
            }
        }
        $region = $db->query("SELECT id FROM region WHERE id IN (".$id.")");
        $region = array_unique(array_column($region,'id'));
        $arr = [];
        $more = [];
        foreach ($region as $k => $v) {
            $arr[] = $this->select("region",['id'=>$v],"id,region_type",1,10000);
        }
        foreach ($arr as $v) {
            switch ($v[0]['region_type']) {
                case 0:
                    $field = "*";
                    break;
                case 1:
                    $field = "province_id";
                    break;
                case 2:
                    $field = "city_id";
                    break;
                case 3:
                    $field = "district_id";
                    break;
            }
            $region_ids = '';
            if($field == '*'){
                $region_ids = "all";
                break;
            } 
            $more[] = $this->select("region",[$field => $v[0]['id']],"id",1,10000);  
        }
        if($region_ids !== "*"){
            $region_ids = [];
            $list = [];
            foreach ($more as $k => $v) {
                $list = array_column($v,'id');
                foreach ($list as $i) {
                    array_push($region_ids, $i);
                }
            }
            $region_ids = implode(',',array_unique($region_ids));
        }
        return $region_ids;
    } 


}